package com.cf.game.stage.npc;

import com.cf.Event;
import com.cf.compent.RollPolingMachine;
import com.cf.core.player.Player;
import com.cf.compent.RandomMachine;
import com.cf.core.event.EndEvent;
import com.cf.core.event.HeartbeatEvent;
import com.cf.core.spirit.Spiriter;
import com.cf.core.stage.Stage;
import com.cf.game.constant.SysConstant;
import com.cf.game.message.BeatResponse;
import com.cf.game.message.PunishNotifyResponse;
import com.cf.game.message.PunishResponse;
import com.cf.game.stage.event.BeatEvent;
import com.cf.game.stage.event.ChangeStatueEvent;
import com.cf.game.stage.event.PunishEvent;
import lombok.extern.slf4j.Slf4j;

/**
 * game-server-Student
 * 玩家
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/5/7 下午 05:56
 */
@Slf4j
public class Student extends Spiriter {

    protected RollPolingMachine polingMachine = new RollPolingMachine(2);

    /**
     * 玩家
     */
    protected Player own;

    /**
     * 敌人
     */
    protected Player enemy;
    /**
     * 学生的状态
     */
    protected String statue = EnumStudentStatue.COMMON.getStatue();

    /**
     * 总分数
     */
    protected int totalScore = 0;

    /**
     * 最近一次击打的事件
     */
    protected Long lastBeatTime = System.currentTimeMillis();

    protected int punishTime = 0;

    protected boolean run = true;

    public Student(Stage stage, Player own, Player enemy) {
        super(stage);
        this.own = own;
        this.enemy = enemy;
    }

    @Override
    public void handle(Event event) {

        if (!run) {
            log.warn("当前游戏已经结束，不处理任何事件了！。。。");
            return;
        }
        if (event instanceof BeatEvent) {
            handleBeat((BeatEvent) event);
        }

        if (event instanceof PunishEvent) {
            log.info("忽略惩罚消息，由classroom对象统一处理");
        }

        if (event instanceof EndEvent) {
            handleEnd((EndEvent) event);
        }

        if (event instanceof HeartbeatEvent) {
            handleHeartbeatEvent((HeartbeatEvent) event);
        }


    }


    /**
     * 处理心跳消息
     *
     * @param event
     */
    protected void handleHeartbeatEvent(HeartbeatEvent event) {
        //更新用户的惩罚状态
        updatePunishTime();
    }

    private void updatePunishTime() {
        if (EnumStudentStatue.PUNISH.getStatue().equals(this.statue)) {
            if (this.punishTime > 0) {
                punishTime -= 1;
            }
            if (this.punishTime == 0) {
                this.statue = EnumStudentStatue.COMMON.getStatue();
                this.getStage().broadcastEvent(new ChangeStatueEvent(this.statue));
            }
        }
    }

    /**
     * 处理结束事件
     *
     * @param event
     */
    protected void handleEnd(EndEvent event) {
        this.clean();
        this.run = false;
    }

    /**
     * 处理惩罚事件
     *
     * @param event
     */
    public boolean handlePunish(PunishEvent event) {
        if (needPunish()) {
            //更新惩罚时间
            this.punishTime = SysConstant.punishTime;
            this.statue = EnumStudentStatue.PUNISH.getStatue();
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判定是否需要惩罚
     *
     * @return
     */
    protected boolean needPunish() {
        return ((System.currentTimeMillis() - lastBeatTime) < SysConstant.punishStep) && EnumStudentStatue.COMMON.getStatue().equals(this.getStage().getStatue()) && EnumStudentStatue.COMMON.getStatue().equals(this.statue);
    }

    /**
     * 处理打击事件
     *
     * @param event
     */
    protected void handleBeat(BeatEvent event) {
        BeatEvent beatEvent = event;
        int beatScore = 0;
        boolean needResponse = beatEvent.needResponse(this.statue, this, this.getStage());
        if (needResponse) {
            if (EnumStudentStatue.COMMON.getStatue().equals(this.statue)) {
                this.totalScore += 1;
                beatScore = 1;
                this.lastBeatTime = System.currentTimeMillis();
            } else {
                if (this.totalScore > 2) {
                    beatScore = -2;
                    this.totalScore -= 2;
                }
            }
            if (polingMachine.ok() && beatScore != 0) {
                this.own.sendMessage(BeatResponse.build(this.statue, this.totalScore, beatScore, true, this.getStage().getStageLeftTime(), this.getStage().getStageTime()));
                this.enemy.sendMessage(BeatResponse.build(this.statue, this.totalScore, beatScore, false, this.getStage().getStageLeftTime(), this.getStage().getStageTime()));
            } else {
                log.info("当前消息不广播给用户！");
            }
        }
    }

    @Override
    public void clean() {
        //让这个对象可以安全的销毁
        own.clean();
    }

    @Override
    public Integer getId() {
        return own.getId();
    }

    public Integer getTotalScore() {
        return totalScore;
    }


    public Player getSelf() {
        return own;
    }

}
